package org.fireflow2.designer.eclipse.parts.tree;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.editparts.AbstractTreeEditPart;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.fireflow.pdl.fpdl20.diagram.Fpdl20DiagramElement;
import org.fireflow2.designer.eclipse.Activator;
import org.fireflow2.designer.eclipse.modelwrapper.AbsWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.Wrapper;
import org.fireflow2.designer.eclipse.modelwrapper.collection.DataFieldListWrapper;
import org.fireflow2.designer.eclipse.policies.datafield.DataFieldListEditPolicy;

public class DataFieldListTreePart extends AbstractTreeEditPart implements
		PropertyChangeListener {
	private Image foldClose = null;
	private Image foldOpen = null;
	public DataFieldListTreePart(){
		ImageDescriptor imageDescriptor = ImageDescriptor.createFromFile(Activator.class, "resources/dataFieldList.png");
		foldClose = imageDescriptor.createImage();
		
		imageDescriptor = ImageDescriptor.createFromFile(Activator.class, "resources/foldopen_16.gif");
		foldOpen = imageDescriptor.createImage();
	}	
	
	protected List getModelChildren() {
		return ((DataFieldListWrapper) getModel()).getChildren();
	}

	protected void refreshVisuals() {
		this.setWidgetText(((Wrapper)this.getModel()).getDisplayName());
		this.setWidgetImage(foldClose);
	}
	@Override
	protected void createEditPolicies() {
		installEditPolicy(EditPolicy.CONTAINER_ROLE,
				new DataFieldListEditPolicy());
	}
	public void propertyChange(PropertyChangeEvent evt) {
		// TODO Auto-generated method stub
		if (evt.getPropertyName().equals(Fpdl20DiagramElement.PROPERTY.name()))
			this.refreshChildren();

	}

	public void activate() {
		if (isActive()) {
			return;
		}
		super.activate();
		((AbsWrapper) getModel()).addPropertyChangeListener(this);
	}

	public void deactivate() {
		if (!isActive()) {
			return;
		}
		super.deactivate();
		((AbsWrapper) getModel()).removePropertyChangeListener(this);
	}
}
